package org.example.service.iml;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.example.service.testService;
import org.example.utils.JsonSortUtil;
import org.example.utils.SM2Utils;
import org.example.utils.SM4Utils;
import org.springframework.beans.factory.annotation.Value;

import javax.jws.WebService;

@Slf4j
@WebService(serviceName = "TestService", // 与接口中指定的name一致
        targetNamespace = "http://service.example.org", // 与接口中的命名空间一致,一般是接口的包名倒
        endpointInterface = "org.example.service.testService"// 接口地址
)
public class testServiceIml implements testService {
    @Value("${sm2Keys.public}")
    private String sm2PublicKey;
    @Value("${sm2Keys.private}")
    private String sm2PrivateKey;
    @Value(("${sm4Keys.secretKey}"))
    private String sm4SecretKey;
    @Override
    public String sayHello( String name) {
        return name+"hello";
    }

    @Override
    public String sm2Encrypt(String data) {
        log.info("Sm2加密数据：",data);
        String str=SM2Utils.encrypt(sm2PublicKey,data);
        System.out.println(str);
        return str;
    }

    @Override
    public String sm2Decrypt(String data) {
        log.info("SM2解密数据：",data);
        String str=SM2Utils.decrypt(sm2PrivateKey,data);
        System.out.println(str);
        return str;
    }

    @Override
    public String sm4EcbEncrypt(String data) {

        log.info("SM4加密数据：",data);
        SM4Utils sm4 = new SM4Utils();
        sm4.secretKey=sm4SecretKey;
        sm4.hexString = true;

        String str=sm4.encryptData_ECB(data);
        System.out.println(str);
        return str;
    }

    @Override
    public String sm4EcbDecrypt(String data) {

        log.info("SM4解密数据：",data);
        SM4Utils sm4 = new SM4Utils();
        sm4.secretKey=sm4SecretKey;
        sm4.hexString = true;
        String str=sm4.decryptData_ECB(data);
        System.out.println(str);
        return str;
    }

    @Override
    public String sortJson(String jsonStr) {
        log.info(jsonStr);
        System.out.println(jsonStr);
        JSONObject jsonObject= JsonSortUtil.startSort((JSONObject) JSONObject.parse(jsonStr));

        return jsonObject.toJSONString();
    }

}
